(function () {

	'use strict';

	angular
		.module('app')
		.controller('OrderDetailController', OrderDetailController);

	OrderDetailController.$inject = ['$scope', '$rootScope','$http', '$window', '$timeout', '$location', '$state', '$stateParams', 'API', 'ENUM', 'PaymentModel', 'OrderExpressModel'];

	function OrderDetailController($scope, $rootScope,$http, $window, $timeout, $location, $state, $stateParams, API, ENUM, PaymentModel, OrderExpressModel) {

		var order_id = $stateParams.order_id;
		var backtype= $stateParams.backtype;

		$scope.order = {};

		$scope.isLoading = false;
		$scope.isLoaded = false;

		$scope.touchPay = function () {
            if (!$scope.order)
                return;

            $scope.paymentModel.clear();
            $state.go('payment', {
                order_id: order_id
            });
        };

		$scope.touchCancel = function() {
            if (!$scope.order)
                return;

            $scope.showDialog = true;
        };

        $scope.touchLive = function(pig_id) {console.log('直播');
            $state.go('video', {
                pig_id: pig_id
            });
        }

		$scope.touchConfirm = function() {
            API.order.confirmReceipt({
                order_id: order_id,
            }).then(function (res) {
                $scope.toast('确认成功');
                _reload();
            });
        };

		$scope.touchExpress = function () {
            $scope.orderExpressModel.clear();
            $scope.orderExpressModel.order = $scope.order;

            $state.go('order-express', {
                order_id: order_id
            });
        };

		$scope.touchComment = function () {
            $state.go('order-review', {
                order_id: order_id
            });
        };

		$scope.touchProduct = function (type, product_id) {
		    if (type ==1 ) {//产品
                $state.go('goods-info', {
                    goods_id: product_id
                });
            } else {//预售
                $state.go('pre-sales-info', {
                    part_id: product_id
                });
            }
        };

		$scope.detailBack = function () {
            if(backtype==1){
                $rootScope.goHome();
            }else{
                $rootScope.goBack()
            }
        };

		$scope.showDialog = false;
		$scope.touchDialogCancel = function () {
            $scope.showDialog = false;
        };

		$scope.touchDialogConfirm = function() {
            API.order.cancel({
                order_id: order_id
            }).then(function (res) {
                $scope.toast('取消成功');
                $scope.showDialog = false;
                _reload();
            });
        };

		$scope.paymentModel = PaymentModel;
		$scope.orderExpressModel = OrderExpressModel;

		function _reload() {
			$scope.isLoading = true;
			$scope.isLoaded = false;
			API.order.getOne({
				order_id: order_id,
			}).then(function (order) {
				$scope.order = order;
				$scope.isLoading = false;
				$scope.isLoaded = true;
			});
		}

		_reload();
	}

})();